package com.yulan.bankback.service.admin.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yulan.bankback.entity.model.admin.IllegalTrading;
import com.yulan.bankback.entity.model.admin.MoneyFlow;
import com.yulan.bankback.entity.model.admin.SensitiveTrading;
import com.yulan.bankback.entity.vo.UserAccountSafeVo;
import com.yulan.bankback.mapper.admin.IllegalTradingMapper;
import com.yulan.bankback.mapper.admin.MoneyFlowMapper;
import com.yulan.bankback.mapper.admin.SensitiveTradingMapper;
import com.yulan.bankback.service.admin.IllegalService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class IllegalServiceImpl implements IllegalService {

    @Resource
    private IllegalTradingMapper illegalTradingMapper;

    @Resource
    private SensitiveTradingMapper sensitiveTradingMapper;

    @Resource
    private MoneyFlowMapper moneyFlowMapper;

    private static final SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    private static final SimpleDateFormat formatter2 = new SimpleDateFormat("yyyy-MM-dd");

    private static final SimpleDateFormat formatter3 = new SimpleDateFormat("MM-dd");

    @Override
    public List<IllegalTrading> getAllIllegalTrading() {
        List<IllegalTrading> res = illegalTradingMapper.selectList(
                new QueryWrapper<IllegalTrading>()
                        .last("limit 500"));
        res.forEach(item -> item.setViewTime(formatter.format(item.getCreateTime())));
        return res;
    }

    @Override
    public List<IllegalTrading> getIllegalTradingByConditions(String start, String end,
                                                              String type, String username, String uid) {
        QueryWrapper<IllegalTrading> queryWrapper = new QueryWrapper<>();
        if(StringUtils.isNotBlank(start)) {
            queryWrapper.gt("create_time", DateUtil.parseDate(start));
        }
        if(StringUtils.isNotBlank(end)) {
            queryWrapper.lt("create_time", DateUtil.parseDate(end));
        }
        if(StringUtils.isNotBlank(type)) queryWrapper.eq("type", type);
        if(StringUtils.isNotBlank(username)) queryWrapper.eq("username", username);
        if(StringUtils.isNotBlank(uid)) queryWrapper.eq("uid", uid);

        List<IllegalTrading> res = illegalTradingMapper.selectList(queryWrapper);
        res.forEach(item -> item.setViewTime(formatter.format(item.getCreateTime())));

        return res;
    }

    @Override
    public UserAccountSafeVo getAccountSafeVo(String interval) {
        UserAccountSafeVo vo = new UserAccountSafeVo();
        //日交易曲线
        List<MoneyFlow> moneyFlows = moneyFlowMapper.getIntervalMoneyFlow(interval);
        List<String> moneyFlowDate = moneyFlows.stream()
                .map(item -> formatter3.format(item.getTime()))
                .collect(Collectors.toList());

        while(moneyFlowDate.size() > 10){
            moneyFlowDate = this.changeListSize(moneyFlowDate, 4);
        }
        vo.setMoneyFlowDate(moneyFlowDate);

        List<Double> moneyFlowData = moneyFlows.stream()
                .map(MoneyFlow::getMoney)
                .collect(Collectors.toList());

        while(moneyFlowData.size() > 10){
            moneyFlowData = this.changeListSize(moneyFlowData, 4);
        }
        vo.setMoneyFlowData(moneyFlowData);

        //敏感交易曲线
        List<SensitiveTrading> sensitiveTradings = sensitiveTradingMapper.getIntervalSensitiveTrading(interval);

        List<String> sensitiveDate = sensitiveTradings.stream().map(item ->
                formatter2.format(item.getTime())).collect(Collectors.toList());

        List<Double> sensitiveData = sensitiveTradings.stream().map(
                SensitiveTrading::getCount).collect(Collectors.toList());

        while(sensitiveDate.size() > 10){
            this.changeListSize(sensitiveDate, 4);
        }

        while (sensitiveData.size() > 10){
            this.changeListSize(sensitiveData,4);
        }

        vo.setSensitiveFlowDate(sensitiveDate);
        vo.setSensitiveFlowData(sensitiveData);

        return vo;

    }

    private <T> List<T> changeListSize(List<T> list, int mod){
        T first = list.get(0);
        T last = list.get(list.size() - 1);

        List<T> res = new ArrayList<>();
        res.add(first);
        for(int i = 1; i < list.size() - 1; i++){
            if(i % mod != 0)
                res.add(list.get(i));
        }
        res.add(last);
        return res;
    }
}
